package com.lc.afbx.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.lc.afbx.BaseApplication;
import com.lc.afbx.R;
import com.lc.afbx.adapters.ShopAdapter;
import com.lc.afbx.conn.GetApiShowStore;
import com.lc.afbx.conn.PostApiSubmitShouXian;
import com.lc.afbx.dialog.MyAlertDialog;
import com.lc.afbx.util.MyToast;
import com.lc.afbx.util.PictureUtil;
import com.lc.afbx.util.PrefrenceConfig;
import com.lc.afbx.widget.MyListView;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilKeyBoard;
import com.zcx.helper.util.UtilLog;
import com.zcx.helper.util.UtilNetWord;
import com.zcx.helper.util.UtilToast;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/7/7.
 */
public class LifeInsuranceInsureActivity extends BaseActivity implements View.OnClickListener {


    private MyListView life_insurance_insure_lv;
    private EditText life_insurance_insure_managerCode_et;
    private ImageView life_insurance_insure_pay_iv;

    private List<GetApiShowStore.Data> lists = new ArrayList<>();
    private ShopAdapter adapter;
    private Bundle bundle;

    private int selectPosition = -1;
    private int payType = 1;// 判断支付方式用  1 银联支付，就特么有银联支付

    // 这是保险门店的接口
    private GetApiShowStore getApiShowStore = new GetApiShowStore(new AsyCallBack<GetApiShowStore.Info>() {

        @Override
        public void onSuccess(String toast, int type, GetApiShowStore.Info info) throws Exception {

            lists.addAll(info.dataList);

            adapter.init();

            adapter.notifyDataSetChanged();

        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            UtilToast.show(context, toast);
        }
    });

    private PostApiSubmitShouXian postApiSubmitShouXian = new PostApiSubmitShouXian(new AsyCallBack<PostApiSubmitShouXian.Info>() {
        @Override
        public void onSuccess(String toast, int type, PostApiSubmitShouXian.Info info) throws Exception {

                new MyAlertDialog(context, "提交成功,\n业务员：" + info.data.nickname + "\n电话：" + info.data.tel + "\n会在24小时内联系您\n请保持电话畅通") {
                    @Override
                    protected void onConfirm() {
                        BaseApplication.INSTANCE.finishActivity(LifeInsuranceActivity.class, LifeInsuranceDetailActivity.class, LifeInsuranceFillInfoActivity.class, LifeInsuranceInsureActivity.class);
                    }
                }.show();

        }

        @Override
        public void onFail(String toast, int type) throws Exception {
            UtilToast.show(context, toast);

        }
    });


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_life_insurance_insure);

        bundle = getIntent().getExtras();

        ((TextView) findViewById(R.id.life_insurance_insure_productName_tv)).setText(bundle.getString("insurance_type"));
        ((TextView) findViewById(R.id.life_insurance_insure_money_tv)).setText(bundle.getString("premium") + "元");

        life_insurance_insure_managerCode_et = ((EditText) findViewById(R.id.life_insurance_insure_managerCode_et));

        life_insurance_insure_pay_iv = ((ImageView) findViewById(R.id.life_insurance_insure_pay_iv));

        if (!TextUtils.isEmpty(BaseApplication.BasePreferences.readCode())) {

            life_insurance_insure_managerCode_et.setEnabled(false);
            life_insurance_insure_managerCode_et.setText(BaseApplication.BasePreferences.readCode());

        }

        life_insurance_insure_lv = ((MyListView) findViewById(R.id.life_insurance_insure_lv));

        life_insurance_insure_lv.setAdapter(adapter = new ShopAdapter(context, lists));

        life_insurance_insure_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                UtilKeyBoard.closeKeybord(life_insurance_insure_managerCode_et, context);

                if (adapter.isCheck.get(position) == false) {
                    adapter.init();
                    adapter.isCheck.put(position, true);
                    selectPosition = position;
                    adapter.notifyDataSetChanged();
                } else {
                    adapter.isCheck.put(position, false);
                    selectPosition = -1;
                    adapter.notifyDataSetChanged();
                }
            }
        });

        getApiShowStore.cid = bundle.getString("insurance_id");
        getApiShowStore.curcode = BaseApplication.BasePreferences.readHomeCityID();
        getApiShowStore.execute(this);


        findViewById(R.id.life_insurance_insure_submit_bt).setOnClickListener(this);
        findViewById(R.id.life_insurance_insure_pay_ll).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {

            case R.id.back:
                finish();
                break;

            case R.id.life_insurance_insure_pay_ll:

                Log.e("------->", "别点了，银联支付");

                break;

            case R.id.life_insurance_insure_submit_bt:

                String managerCode = life_insurance_insure_managerCode_et.getText().toString().trim();

                if (selectPosition < 0) {
                    UtilToast.show(this, "请选择一个投保门店");
                    return;
                }

                String storeId = lists.get(selectPosition).id;

//                UtilLog.e("投保人身份证路径---------", bundle.getString("applica_idUrl"));
//                UtilLog.e("投保人照片路径---------", bundle.getString("applica_peopleUrl"));

                postApiSubmitShouXian.applicant_information = bundle.getString("applicant_information");
                postApiSubmitShouXian.id_url = PictureUtil.bitmapToString(bundle.getString("applica_idUrl"));
                postApiSubmitShouXian.self_url = PictureUtil.bitmapToString(bundle.getString("applica_peopleUrl"));

                // 判断其他被保人是不是本人  不是本人 就那获取json  存图片 然后在提交  ，是本人 提交 空数据
                if (bundle.getString("relation_yuBeiBao").equals("2")) {

//                    UtilLog.e("其他投保人身份证路径---------", bundle.getString("other_idUrl"));
//                    UtilLog.e("其他投保人身份证路径---------", bundle.getString("other_peopleUrl"));

                    postApiSubmitShouXian.other_insureds = bundle.getString("other_insureds");
                    postApiSubmitShouXian.otherid_url = PictureUtil.bitmapToString(bundle.getString("other_idUrl"));
                    postApiSubmitShouXian.otherself_url = PictureUtil.bitmapToString(bundle.getString("other_peopleUrl"));

                } else {
                    postApiSubmitShouXian.other_insureds = "";
                    postApiSubmitShouXian.otherid_url = "";
                    postApiSubmitShouXian.otherself_url = "";
                }

                postApiSubmitShouXian.userid = BaseApplication.BasePreferences.readUserId();
                postApiSubmitShouXian.product_id = bundle.getString("product_id");
                postApiSubmitShouXian.starttime = bundle.getString("starttime", "===========");
                postApiSubmitShouXian.copies = bundle.getString("copies", "===========");
                postApiSubmitShouXian.payment_way = bundle.getString("payment_way", "===========");
                postApiSubmitShouXian.relation = bundle.getString("relation_yuBeiBao", "===========");
                postApiSubmitShouXian.death_beneficiary = bundle.getString("death_beneficiary", "===========");
                postApiSubmitShouXian.stores_id = storeId;
                postApiSubmitShouXian.manager_code = managerCode;

                postApiSubmitShouXian.execute(this);



                Log.e("payment_way", bundle.getString("payment_way", "==========="));


                //这注释干啥的 留着吧
//                    postSubmitApply.userid = BaseApplication.BasePreferences.readUserId();
//                    postSubmitApply.applicant_information = bundle.getString("applicant_information", "===========");
//                    postSubmitApply.relation = bundle.getString("relation_yuBeiBao", "===========");
//                    postSubmitApply.other_insureds = bundle.getString("other_insureds", "==========");
//                    postSubmitApply.death_beneficiary = bundle.getString("death_beneficiary", "===========");
//
//                    postSubmitApply.insurance_type = bundle.getString("insurance_type", "===========");
//                    postSubmitApply.copies = bundle.getString("copies", "===========");
//                    postSubmitApply.premium = bundle.getString("premium", "===========");
//                    postSubmitApply.payment_way = bundle.getString("payment_way", "===========");
//                    postSubmitApply.payment_is = bundle.getString("payment_is", "===========");
//
//                    postSubmitApply.cid = bundle.getString("insurance_id", "===========");
//                    postSubmitApply.stores_id = storeId;
//                    postSubmitApply.starttime = bundle.getString("starttime", "===========");
//                    postSubmitApply.time_limit = bundle.getString("time_limit", "===========");
//                    postSubmitApply.manager_code = managerCode.getText().toString();
//
//                    postSubmitApply.execute(this);

                break;
        }

    }

}
